////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   CATALOG_SECURITY
// AUTHOR           :   Saint Peng
// DATE CREATE      :   2007/03/29
// DESCRIPTION      :   a DAO file for the function, CATALOG_SECURITY
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/03/29   Saint Peng      1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.catalog.catalogSecurity.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.catalog.catalogSecurity.pk.CatalogSecurityDtlPk;
import com.erp.app.catalog.catalogSecurity.pk.CatalogSecurityHdrPk;
import com.erp.app.catalog.catalogSecurity.vo.CatalogSecurityDtlVo;
import com.erp.app.common.ProjectConstants;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.dao.SimpleSearchMaintStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.CommonUtil;

/**
* <p>A <strong>DAO</strong>(Data Access Object) abstracts and encapsulates
* all access to the persistent store.  The DAO manages the connection with the
* data source to obtain and store data.  Also, the DAO implements
* the access mechanism required to work with the data source,
* and always provides a uniform API to its clients.
*
* @author          Saint Peng
* @version 1.0     2007/03/29
* @since           FrameWork 1.0
*
*/
public class CatalogSecurityDtlDao extends SimpleSearchMaintStyleDao {

	/**
	* Returns an array list of child VOs by the parent
	* @param         parent   the incoming parent VO
	* @param         conn     the database connection
	* @return        an array list of child VOs
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	public ChildList selectByParent(SimpleBaseVo parent, Connection conn)
		throws FindRecordException {

		return super.selectByParent(parent, CatalogSecurityDtlVo.class, conn);
	}

	/**
	* Constructs a string of SQL insert statement
	* @return        a string of SQL insert statement
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected String sqlInsert() {

		String sql = "INSERT INTO T_CATALOG_SECURITY_DTL ("
			+ "COMPANY_CODE, USER_CODE, NODE_ID, CREATE_USER, CREATE_DATE) "
			+ "VALUES ("
			+ "?, ?, ?, ?, SYSDATE )";
		return sql;
	}

	/**
	* Sets the values for a SQL insert or update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected void setValuesForInsertAndModify(PreparedStatement pStmt, Object obj, String opType)
		throws SQLException {

		CatalogSecurityDtlVo vo = (CatalogSecurityDtlVo) obj;
		int i = 1;
		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getUserCode());
		pStmt.setInt(i++, CommonUtil.strToBigDecimal(vo.getDtlNodeId()).intValue());
		setNullableString(pStmt, i++, vo.getCreateUser());

		if (ProjectConstants.DB_OP_UPDATE.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getCreateDate());


		if(ProjectConstants.DB_OP_UPDATE.equals(opType)){
			pStmt.setString(i++, vo.getCompanyCode());
			pStmt.setString(i++, vo.getUserCode());
			pStmt.setInt(i++, CommonUtil.strToBigDecimal(vo.getDtlNodeId()).intValue());
		}
	}

	/**
	* Sets the values for a SQL insert statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected void setValuesForInsert(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_INSERT);
	}

	/**
	* Constructs a string of SQL update statement
	* @return        a string of SQL update statement
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected String sqlUpdate() {

		String sql = "UPDATE T_CATALOG_SECURITY_DTL SET company_code = ? , user_code=?, NODE_ID=?, CREATE_USER=?, CREATE_DATE=? "
			+ "WHERE company_code = ? and user_code=? AND NODE_ID=? ";

		return sql;
	}

	/**
	* Sets the values for a SQL update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected void setValuesForModify(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_UPDATE);
	}

	/**
	* Constructs a string of SQL delete statement
	* @return        a string of SQL update statement
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected String sqlDelete() {
		String sql = "DELETE FROM T_CATALOG_SECURITY_DTL WHERE company_code = ? and USER_CODE=? AND NODE_ID=? ";

		return sql;
	}

	/**
	* Sets the values for a SQL delete statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected void setValuesForDelete(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CatalogSecurityDtlVo vo = (CatalogSecurityDtlVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getUserCode());
		pStmt.setInt(i++, CommonUtil.strToBigDecimal(vo.getDtlNodeId()).intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected String sqlFind() {

		String sql = "SELECT A.COMPANY_CODE, A.USER_CODE, A.NODE_ID, A.CREATE_USER, A.CREATE_DATE, A.UPDATE_USER, A.UPDATE_DATE, " +
		 " B.DESCRIPTION,B.CATALOG_LEVEL "+
		 " FROM   T_CATALOG_SECURITY_DTL A,T_CATALOG B"+
		 " WHERE  A.Company_code = ? and A.USER_CODE = ? AND A.NODE_ID=? "+
		 " AND A.COMPANY_CODE=B.COMPANY_CODE "+
		 " AND A.NODE_ID=B.NODE_ID ";
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding by primary key.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming PK
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected void setValuesForFind(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CatalogSecurityDtlPk pk = (CatalogSecurityDtlPk) obj;
		int i = 1;

		pStmt.setString(i++, pk.getCompanyCode());
		pStmt.setString(i++, pk.getUserCode());
		pStmt.setInt(i++, CommonUtil.strToBigDecimal(pk.getDtlNodeId()).intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected String sqlList() {
		String sql = "SELECT A.COMPANY_CODE, A.USER_CODE, A.NODE_ID, A.CREATE_USER, A.CREATE_DATE, A.UPDATE_USER, A.UPDATE_DATE, " +
		 " B.DESCRIPTION,B.CATALOG_LEVEL "+
		 " FROM   T_CATALOG_SECURITY_DTL A,T_CATALOG B "+
		 " WHERE  A.Company_code = ? and A.USER_CODE = ?  "+
		 " AND A.COMPANY_CODE=B.COMPANY_CODE "+
		 " AND A.NODE_ID=B.NODE_ID ";


		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding child tables.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming parent PK
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected void setValuesForList(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CatalogSecurityHdrPk pk = (CatalogSecurityHdrPk) obj;
		int i = 1;
		pStmt.setString(i++, pk.getCompanyCode());
		pStmt.setString(i++, pk.getUserCode());
	}

	/**
	* Sets the values of a result set to a vo
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Saint Peng
	* @since      Framework 1.0
	*/
	protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {

		CatalogSecurityDtlVo vo = new CatalogSecurityDtlVo();
		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setUserCode(rs.getString("USER_CODE"));
		vo.setDtlNodeId(CommonUtil.bigDecimalToStr(rs.getBigDecimal("NODE_ID")));
		vo.setDtlCatalogLevel(rs.getString("CATALOG_LEVEL"));
		vo.setDtlCatalogName(rs.getString("DESCRIPTION"));
		vo.setCreateUser(rs.getString("CREATE_USER"));
		vo.setCreateDate(rs.getTimestamp("CREATE_DATE"));
		vo.setUpdateUser(rs.getString("UPDATE_USER"));
		vo.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));


		return vo;
	}

	protected SimpleBaseVo getResultSetValuesForFind(ResultSet rs) throws SQLException {
		CatalogSecurityDtlVo vo = new CatalogSecurityDtlVo();
		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setUserCode(rs.getString("USER_CODE"));
		vo.setDtlNodeId(CommonUtil.bigDecimalToStr(rs.getBigDecimal("NODE_ID")));
		vo.setCreateUser(rs.getString("CREATE_USER"));
		vo.setCreateDate(rs.getTimestamp("CREATE_DATE"));
		vo.setUpdateUser(rs.getString("UPDATE_USER"));
		vo.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));


		return vo;
	}





}
